package com.linjian.togetherbirthday.fragment;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.linjian.togetherbirthday.MainActivity;
import com.linjian.togetherbirthday.R;
import com.linjian.togetherbirthday.base.BaseFragment;
import com.linjian.togetherbirthday.contant.HttpContant;
import com.linjian.togetherbirthday.utils.HttpUtils;
import com.linjian.togetherbirthday.utils.NoticeEvent;
import com.linjian.togetherbirthday.utils.SharedPreferencesUtil;
import com.sdsmdg.tastytoast.TastyToast;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.IOException;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class HomeFourFragment extends BaseFragment implements View.OnClickListener {

    private TextView releaseTv,onADateTv,registManagerTv;
    private ImageView userPic,setIv;
    private RelativeLayout editRl;

    private MainActivity activity;

    public static HomeFourFragment newInstance() {
        HomeFourFragment homeOneFragment = new HomeFourFragment();
        Bundle bundle = new Bundle();
//        bundle.putInt(Integer.class.getName(), resId);
        homeOneFragment.setArguments(bundle);
        return homeOneFragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        EventBus.getDefault().register(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getActivity().getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }

    }


    @Override
    protected int setView() {
        return R.layout.fragment_home_four;
    }

    @Override
    protected void init(View view) {
        activity = (MainActivity) getActivity();
        editRl = view.findViewById(R.id.me_edit_rl);
        setIv = view.findViewById(R.id.me_set_tv);
        userPic = view.findViewById(R.id.me_user_pic_iv);
        releaseTv = view.findViewById(R.id.me_dynamic_tc);
        onADateTv = view.findViewById(R.id.me_on_a_date_tv);
        registManagerTv = view.findViewById(R.id.me_regist_manager_tv);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        Map<String, Object> map = new Gson().fromJson(String.valueOf(SharedPreferencesUtil.get(getActivity(), "LoginInfo", "")), Map.class);
        Glide.with(getActivity()).load(map.get("headImg")).asBitmap().error(R.mipmap.user_pic).placeholder(R.mipmap.user_pic).into(userPic);
        setListener();
        httpPersonInfo();
    }

    private void setListener() {
        editRl.setOnClickListener(this);
        setIv.setOnClickListener(this);
        userPic.setOnClickListener(this);
        releaseTv.setOnClickListener(this);
        onADateTv.setOnClickListener(this);
        registManagerTv.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.me_edit_rl:
                activity.replaceFragment("EditFragment","");
                break;
            case R.id.me_set_tv:
                activity.replaceFragment("SetFragment","");
                break;
            case R.id.me_user_pic_iv:

                break;
            case R.id.me_dynamic_tc:
                activity.replaceFragment("MyDynamicFragment","");
                break;
            case R.id.me_on_a_date_tv:
                activity.replaceFragment("MyOnADateFragment","");
                break;
            case R.id.me_regist_manager_tv:
                activity.replaceFragment("RegistManagerFragment","");
                break;
        }
    }

    /**
     * 查询个人信息
     */
    private void httpPersonInfo() {
        HttpUtils.doGet(HttpContant.URL + HttpContant.PersonInfo
                , "SESSION=" + String.valueOf(SharedPreferencesUtil.get(getActivity(), "Cookie", ""))
                , new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        EventBus.getDefault().post(new NoticeEvent("requestFail"));
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        EventBus.getDefault().post(new NoticeEvent("getPersonInfo",response.body().string()));
                    }
                });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMsg(NoticeEvent event){
        switch (event.getCls()){
            case "requestFail":
                TastyToast.makeText(getActivity(),"请求失败",TastyToast.LENGTH_SHORT,TastyToast.ERROR).show();
                break;
            case "getPersonInfo":
                Log.i("getPersonInfo",event.getData().toString());
                break;
        }
    }
}
